package com.android.common.flutter;

import android.text.TextUtils;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.android.common.R;
import com.android.common.constant.Constant;
import com.android.common.mvp.model.CodeMsgBean;
import com.android.common.utils.GsonUtil;
import io.flutter.plugin.common.BasicMessageChannel;

/**
 * 用于传递字符串和半结构化的信息（双向有返回值）
 */
public class BaseBasicMessageChannelHandler implements BasicMessageChannel.MessageHandler<String>{
    @Override
    public void onMessage(@Nullable String message, @NonNull BasicMessageChannel.Reply<String> reply) {
        CodeMsgBean receiveBean = GsonUtil.StringToObject(message, CodeMsgBean.class);
        if (TextUtils.isEmpty(receiveBean.getMessage())){
            Toast.makeText(Constant.GLOBAL_APPLICATION,Constant.GLOBAL_APPLICATION.getString(R.string.msg_not_empty),Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(Constant.GLOBAL_APPLICATION,receiveBean.getMessage(),Toast.LENGTH_SHORT).show();
            reply.reply(GsonUtil.ObjectToString(CodeMsgBean.success()));
        }
    }
}
